package rfc3962

import (
	
	
	

	
	
)

const (
	s2kParamsZero = 4294967296
)

// StringToKey returns a key derived from the string provided according to the definition in RFC 3961.
func (, ,  string,  etype.EType) ([]byte, error) {
	,  := S2KparamsToItertions()
	if  != nil {
		return nil, 
	}
	return StringToKeyIter(, , , )
}

// StringToPBKDF2 generates an encryption key from a pass phrase and salt string using the PBKDF2 function from PKCS #5 v2.0
func (,  string,  int64,  etype.EType) []byte {
	return pbkdf2.Key64([]byte(), []byte(), , int64(.GetKeyByteSize()), .GetHashFunc())
}

// StringToKeyIter returns a key derived from the string provided according to the definition in RFC 3961.
func (,  string,  int64,  etype.EType) ([]byte, error) {
	 := .RandomToKey(StringToPBKDF2(, , , ))
	return .DeriveKey(, []byte("kerberos"))
}

// S2KparamsToItertions converts the string representation of iterations to an integer
func ( string) (int64, error) {
	//The s2kparams string should be hex string representing 4 bytes
	//The 4 bytes represent a number in big endian order
	//If the value is zero then the number of iterations should be 4,294,967,296 (2^32)
	var  uint32
	if len() != 8 {
		return int64(s2kParamsZero), errors.New("invalid s2kparams length")
	}
	,  := hex.DecodeString()
	if  != nil {
		return int64(s2kParamsZero), errors.New("invalid s2kparams, cannot decode string to bytes")
	}
	 = binary.BigEndian.Uint32()
	return int64(), nil
}